/**
  * @title:NotifyManager.java
  * TODO包含类名列表
  * Copyright (C) Shenzhen Futuo Technology Co.Ltd.All right reserved.
  * @version：v1.0,2015-8-31
  */
package helen.core.tools;

import java.io.IOException;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.json.JSONException;
import org.json.JSONObject;

import android.R;
import android.annotation.SuppressLint;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Vibrator;
import android.util.Log;


/**@name NotifyManager
 * @description 用于实现通知栏通的类
 * @author max
 * @date 2015-8-31
 */
public class NotifyManager
{
	private NotificationManager nm;
	private Context context = null;
	private MediaPlayer mMediaPlayer = null;
	private Vibrator vibrator = null;
	private int showCount = 0;
	private boolean isVector;
	private boolean isRing;
	
	
	public NotifyManager(Context context)
	{
		// TODO Auto-generated constructor stub
		this.context = context;
	}
	
	@SuppressLint("NewApi")
	private void show(String title,String ticker,String content){
		//定义NotificationManager
		nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		 //此Builder为android.support.v4.app.NotificationCompat.Builder中的，下同。
        Builder mBuilder = new Builder(context);
        //系统收到通知时，通知栏上面显示的文字。
        mBuilder.setTicker(ticker);
        //通知标题
        mBuilder.setContentTitle(title);
        //通知内容
        mBuilder.setContentText(content);
        //设置大图标，即通知条上左侧的图片（如果只设置了小图标，则此处会显示小图标）
        mBuilder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.btn_plus));
        //显示在小图标左侧的数字
        mBuilder.setNumber(showCount);
        //显示通知，id必须不重复，否则新的通知会覆盖旧的通知（利用这一特性，可以对通知进行更新）
        nm.notify(1, mBuilder.build());
        if(isVector){
        	vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        	vibrator.vibrate(1500);
        }
        
        if(isRing){
        	PlaySound(context);
        }
	}

	@SuppressLint("NewApi")
	private void showWithId(String title,String ticker,String content,int id){
		//定义NotificationManager
				nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
				 //此Builder为android.support.v4.app.NotificationCompat.Builder中的，下同。
		        Builder mBuilder = new Builder(context);
		        //系统收到通知时，通知栏上面显示的文字。
		        mBuilder.setTicker(ticker);
		        //通知标题
		        mBuilder.setContentTitle(title);
		        //通知内容
		        mBuilder.setContentText(content);
		        //设置大图标，即通知条上左侧的图片（如果只设置了小图标，则此处会显示小图标）
		        mBuilder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.btn_plus));
		        //显示在小图标左侧的数字
		        mBuilder.setNumber(showCount);
		        //显示通知，id必须不重复，否则新的通知会覆盖旧的通知（利用这一特性，可以对通知进行更新）
		        nm.notify(id, mBuilder.build());
		        if(isVector){
		        	vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
		        	vibrator.vibrate(1500);
		        }
		        
		        if(isRing){
		        	PlaySound(context);
		        }
	}
	
	public  void PlaySound(final Context context) {  
        
        Log.e("ee", "正在响铃");  
        // 使用来电铃声的铃声路径  
        Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);   
        // 如果为空，才构造，不为空，说明之前有构造过  
        if(mMediaPlayer == null)  
            mMediaPlayer = new MediaPlayer();  
        try {  
            mMediaPlayer.setDataSource(context, uri);  
            mMediaPlayer.setLooping(true); //循环播放  
            mMediaPlayer.prepare();  
            mMediaPlayer.start(); 
            Timer timer = new Timer();
            timer.schedule(new TimerTask()
			{
				
				@Override
				public void run()
				{
					// TODO Auto-generated method stub
					mMediaPlayer.stop(); 
					if(mMediaPlayer != null){  
			            mMediaPlayer.release();  
			        }
					mMediaPlayer = null;
				}
			}, 400);
            
        } catch (IllegalArgumentException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (SecurityException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (IllegalStateException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (IOException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
          
    }  
	
	public void close(){
		if(nm != null){
			nm.cancel(1);
		}
		
        if(vibrator != null){  
            vibrator.cancel();  
        }  
	}

}

